package com.hyt.it.ogt.kq.common.gov.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Matcher;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *
 * </p>
 *
 * @author Chengxh
 * @since 2021-08-14
 */
@Slf4j
public class DateTimeUtil extends org.apache.commons.lang3.time.DateUtils {

    private static final DateTimeFormatter DF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    private static final DateTimeFormatter DF_SHORT = DateTimeFormatter.ofPattern("yyMMdd");
    private static String[] parsePatterns = {
			"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
			"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取日期-年-位数不固定
     * @param figures
     * @return
     */
    public static String getFormatYear(Integer figures) {
        String year = String.valueOf(LocalDate.now().getYear());
        int len = 4;
        if (figures > len || figures < 0) {
            return year;
        }

        return year.substring(4 - figures,4);
    }

    public static LocalDateTime getFormatDateTime(String dateTime) {

        return LocalDateTime.parse(dateTime,DF);
    }

    public static LocalDateTime getFormatNow() {

        return LocalDateTime.parse(LocalDateTime.now().format(DF),DF);
    }

    public static String getDateShort(LocalDateTime dataTime) {
        return dataTime.format(DF_SHORT);
    }

    public static String getDateTimeString(LocalDateTime dataTime) {
        return dataTime.format(DF);
    }

    @SuppressWarnings("deprecation")
	public static double date2double(Date date) {
        long offset = date.getTimezoneOffset() * 60000L;
        double dd = (double) (date.getTime() - offset) / 24 / 3600 / 1000 + 25569.0000000;
        DecimalFormat df = new DecimalFormat("#.0000000000");
        return Double.parseDouble(df.format(dd));
    }

    public static Date asDate(LocalDateTime localDateTime) {
    	if(null == localDateTime) {
    		return null;
    	}
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 时间格式：yyyyMMddHHmmssSSS
     * @return
     */
    public static String getNowString() {
        return LocalDateTime.now().format(DATETIME_FORMATTER);
    }

    /**
     * 验证是否为生日<br>
     * 只支持以下几种格式：
     * <ul>
     * <li>yyyyMMdd</li>
     * <li>yyyy-MM-dd</li>
     * <li>yyyy/MM/dd</li>
     * <li>yyyy.MM.dd</li>
     * <li>yyyy年MM月dd日</li>
     * </ul>
     *
     * @param value 值
     * @return 是否为生日
     */
    public static boolean isBirthday(CharSequence value) {
        final Matcher matcher = Validator.BIRTHDAY.matcher(value);
        if (matcher.find()) {
            int year = Integer.parseInt(matcher.group(1));
            int month = Integer.parseInt(matcher.group(3));
            int day = Integer.parseInt(matcher.group(5));
            return isBirthday(year, month, day);
        }
        return false;
    }

    /**
     * 验证是否为生日
     *
     * @param year  年，从1900年开始计算
     * @param month 月，从1开始计数
     * @param day   日，从1开始计数
     * @return 是否为生日
     */
    public static boolean isBirthday(int year, int month, int day) {
        // 验证年
        int thisYear = DateUtil.thisYear();
        if (year < 1900 || year > thisYear) {
            return false;
        }

        // 验证月
        if (month < 1 || month > 12) {
            return false;
        }

        // 验证日
        if (day < 1 || day > 31) {
            return false;
        }
        int thisDay = DateUtil.thisDayOfMonth();
        int thisMonth = DateUtil.thisMonth() + 1;
        if ((year == thisYear) && (month == thisMonth) && (day > thisDay)) {
            return false;
        }

        // 检查几个特殊月的最大天数
        if (day == 31 && (month == 4 || month == 6 || month == 9 || month == 11)) {
            return false;
        }
        if (month == 2) {
            // 在2月，非闰年最大28，闰年最大29
            return day < 29 || (day == 29 && DateUtil.isLeapYear(year));
        }
        return true;
    }

    /**
     * 日期型字符串转化为日期 格式
     * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     *   "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
     *   "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
     */
    public static Date parseDate(Object str) {
        if (str == null){
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    public static void main(String[] args) {

        //

        //

        LocalDateTime start = getFormatDateTime("2021-11-25 16:29:56");

        LocalDateTime end = getFormatDateTime("2021-11-25 16:32:06");


        System.out.println(getFormatNow());
    }
}
